/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import bean.Cliente;
import bean.Produto;
import bean.Vendas;
import bean.VendasItem;
import bean.VendasItemPK;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
import org.hibernate.Session;
import util.HibernateUtil;

/**
 *
 * @author Oziel
 */
public class viewVendas extends javax.swing.JFrame {

    /**
     * Creates new form viewVendas
     */
    public viewVendas() {
        //Look And Feell
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            SwingUtilities.updateComponentTreeUI(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        initComponents();
        this.setLocationRelativeTo(this);
        idGenerator();

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToggleButton1 = new javax.swing.JToggleButton();
        jPanel1 = new javax.swing.JPanel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel3 = new javax.swing.JPanel();
        jTNumeroVendas = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTCodClienteVendas = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTNomeVendas = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTObservacaoVendas = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jDecriProduto = new javax.swing.JTextField();
        jValorU = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jProdutoCod = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jQtdeProduto = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jTotal = new javax.swing.JTextField();
        jBIniciarVendas = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jTDataVendas = new javax.swing.JFormattedTextField();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableVendas = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();

        jToggleButton1.setText("jToggleButton1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Lançamento de Vendas"));

        jTNumeroVendas.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTNumeroVendasFocusLost(evt);
            }
        });

        jLabel1.setText("Número");

        jLabel2.setText("Data - MM/dd/yy");

        jTCodClienteVendas.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTCodClienteVendasFocusLost(evt);
            }
        });

        jLabel3.setText("Cliente");

        jLabel5.setText("Nome");

        jLabel4.setText("Observação");

        jValorU.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jValorUFocusLost(evt);
            }
        });
        jValorU.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jValorUKeyPressed(evt);
            }
        });

        jLabel9.setText("Valor Unitário:");

        jLabel7.setText("Descrição:");

        jProdutoCod.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jProdutoCodFocusLost(evt);
            }
        });

        jLabel6.setText("Produto:");

        jLabel8.setText("Quantidade:");

        jLabel10.setText("Total R$:");

        jBIniciarVendas.setText("Nova Venda");
        jBIniciarVendas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBIniciarVendasActionPerformed(evt);
            }
        });

        jButton2.setText("Salvar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        try {
            jTDataVendas.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jLabel4)
                        .addGroup(jPanel3Layout.createSequentialGroup()
                            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jTNumeroVendas, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel1))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jTDataVendas, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel2))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel3)
                                .addComponent(jTCodClienteVendas, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(23, 23, 23)
                            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel5)
                                .addComponent(jTNomeVendas, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addComponent(jTObservacaoVendas))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jQtdeProduto)
                            .addComponent(jProdutoCod, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jDecriProduto, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jValorU, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                            .addComponent(jBIniciarVendas)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton2))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                            .addGap(12, 12, 12)
                            .addComponent(jLabel10)
                            .addGap(7, 7, 7)
                            .addComponent(jTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTNumeroVendas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTCodClienteVendas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTNomeVendas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTDataVendas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTObservacaoVendas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jProdutoCod, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(jDecriProduto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jQtdeProduto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(jValorU, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(jTotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 61, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBIniciarVendas)
                    .addComponent(jButton2))
                .addGap(27, 27, 27))
        );

        jTabbedPane1.addTab("Vendas", jPanel3);

        jTableVendas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Produto", "Descrição", "Qtde", "Valor Unitário", "Total R$"
            }
        ));
        jTableVendas.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(jTableVendas);
        jTableVendas.getColumnModel().getColumn(0).setResizable(false);
        jTableVendas.getColumnModel().getColumn(1).setResizable(false);
        jTableVendas.getColumnModel().getColumn(2).setResizable(false);
        jTableVendas.getColumnModel().getColumn(3).setResizable(false);
        jTableVendas.getColumnModel().getColumn(4).setResizable(false);

        jButton1.setText("Mostrar Vendas");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jButton1)
                .addGap(0, 0, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 499, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addGap(0, 29, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Vendas Lançadas", jPanel4);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 504, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTNumeroVendasFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTNumeroVendasFocusLost
    }//GEN-LAST:event_jTNumeroVendasFocusLost

    private void jTCodClienteVendasFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTCodClienteVendasFocusLost
        if (jTCodClienteVendas.getText().equals("") || jTCodClienteVendas.getText().trim() == null) {
        } else {

            Boolean verif = false;
            Session session = HibernateUtil.getSession();
            List<Cliente> listacli = session.createCriteria(Cliente.class).list();
            for (Cliente compras : listacli) {
                if (jTNumeroVendas.getText().trim().equals(Integer.toString(compras.getCliCod())) || (jTCodClienteVendas.getText().trim().equals(Integer.toString(compras.getCliCod())))) {
                    jTNomeVendas.setText(compras.getCliNom());
                    verif = true;
                }
            }

            if (verif == false) {
                JOptionPane.showMessageDialog(null, "Cliente Não Encontrado!");
                jTCodClienteVendas.requestFocus();
                jTCodClienteVendas.setText(null);
                jTNomeVendas.setText(null);
            }
            session.flush();
            session.close();
        }
    }//GEN-LAST:event_jTCodClienteVendasFocusLost

    private void jBIniciarVendasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBIniciarVendasActionPerformed
        limpaCampo();
    }//GEN-LAST:event_jBIniciarVendasActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        Commit();

    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        preencherTable();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jProdutoCodFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jProdutoCodFocusLost
        Boolean t = false;
        Session session = HibernateUtil.getSession();
        List<Produto> listaprod = session.createCriteria(Produto.class).list();
        for (Produto produtos : listaprod) {
            if (jProdutoCod.getText().trim().equals(Integer.toString(produtos.getProCod()))) {
                jDecriProduto.setText(produtos.getProDes());
                t = true;
            }
        }
        if (t == false) {
            JOptionPane.showMessageDialog(null, "Produto Nao Encontrado!");
            jDecriProduto.setText(null);
            jProdutoCod.requestFocus();
        }

        session.flush();

        session.close();

    }//GEN-LAST:event_jProdutoCodFocusLost

    private void jValorUFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jValorUFocusLost
        String valor = Double.toString(Double.parseDouble(jQtdeProduto.getText()) * Double.parseDouble(jValorU.getText()));
        jTotal.setText(valor);

    }//GEN-LAST:event_jValorUFocusLost

    private void jValorUKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jValorUKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_jValorUKeyPressed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;

                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(viewVendas.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(viewVendas.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(viewVendas.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(viewVendas.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new viewVendas().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBIniciarVendas;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JTextField jDecriProduto;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JTextField jProdutoCod;
    private javax.swing.JTextField jQtdeProduto;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTCodClienteVendas;
    private javax.swing.JFormattedTextField jTDataVendas;
    private javax.swing.JTextField jTNomeVendas;
    private javax.swing.JTextField jTNumeroVendas;
    private javax.swing.JTextField jTObservacaoVendas;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable jTableVendas;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JTextField jTotal;
    private javax.swing.JTextField jValorU;
    // End of variables declaration//GEN-END:variables

    public void preencherTable() {
        Boolean Verif = false;
        DefaultTableModel modelo = (DefaultTableModel) jTableVendas.getModel();
        Session session = HibernateUtil.getSession();
        List<Produto> listaP = session.createCriteria(Produto.class).list();// Irá fazer uma Lista
        List<VendasItem> lista = session.createCriteria(VendasItem.class).list();// Iráma Lista fazer u
        modelo.setNumRows(0);
        for (VendasItem vendasItem : lista) {
            for (Produto produto : listaP) {
                if (vendasItem.getVendasItemPK().getComPro() == produto.getProCod()) {
                    modelo.addRow(new Object[]{vendasItem.getVendasItemPK().getComCod(), produto.getProDes(), vendasItem.getComQtd(), vendasItem.getComVlr(), (vendasItem.getComQtd() * vendasItem.getComVlr())});
                    Verif = true;
                }

            }

        }
        if (Verif == false) {
            JOptionPane.showMessageDialog(null, "Nenhum Registro Encontrado!");
        }
    }

    public void limpaCampo() {
        idGenerator();

        jTDataVendas.setText(null);
        jTCodClienteVendas.setText(null);
        jTObservacaoVendas.setText(null);
        jTNomeVendas.setText(null);
        jProdutoCod.setText(null);
        jDecriProduto.setText(null);
        jQtdeProduto.setText(null);
        jValorU.setText(null);
        jTotal.setText(null);
    }

    public void Commit() {
        Session session = HibernateUtil.getSession();
        Vendas vendas = new Vendas();
        VendasItem Vendasitem = new VendasItem();
        VendasItemPK Vendasitempk = new VendasItemPK();

        String data = jTDataVendas.getText().replaceAll("[/]", "");
        DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
        try {
            vendas.setComDta(formatter.parse(data));
        } catch (ParseException ex) {
            Logger.getLogger(viewCompras.class.getName()).log(Level.SEVERE, null, ex);
        }

        vendas.setComCli(Integer.parseInt(jTCodClienteVendas.getText()));

        vendas.setComObs(jTObservacaoVendas.getText());
        //Gerador de ID do ComprasItens
        int i = 1;
        List<VendasItem> lista = session.createCriteria(VendasItem.class).list();// Irá fazer uma Lista
        for (VendasItem vendasitem : lista) {
            i++;
        }

        //FIM
        Vendasitempk.setComCod(i);
        Vendasitempk.setComPro(Integer.parseInt(jProdutoCod.getText()));
        Vendasitem.setComQtd(Double.parseDouble(jQtdeProduto.getText()));
        Vendasitem.setComVlr(Double.parseDouble(jValorU.getText()));
        Vendasitem.setVendasItemPK(Vendasitempk);
        //Commit no banco de dados.
        session.beginTransaction();
        session.saveOrUpdate(vendas);
        session.saveOrUpdate(Vendasitem);

        session.getTransaction().commit();
        JOptionPane.showMessageDialog(null, "Cadastrado com Sucesso!");
        session.flush();
        limpaCampo();
        session.close();

    }
    //Gerador Automatico de ID.

    private void idGenerator() {
        jTNumeroVendas.setEnabled(false);
        jDecriProduto.setEnabled(false);
        jTDataVendas.setEnabled(false);
        int i = 1;
        Session session = HibernateUtil.getSession();

        List<Vendas> lista = session.createCriteria(Vendas.class).list();
        for (Vendas cadVendas : lista) {
            i = cadVendas.getComCod() + 1;

        }
        jTNumeroVendas.setText(Integer.toString(i));
        Date data = new Date(System.currentTimeMillis());
        SimpleDateFormat formatarDate = new SimpleDateFormat("MM/dd/yyyy");//Data - MM/dd/yy
        jTDataVendas.setText(formatarDate.format(data));
    }
}
